import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.WebContext;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

class Image{
    public String name;
    public String url;

}
@WebServlet("/image")
public class ImageServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html; charset=utf-8");
        List<Image> images=loadImage();
        ServletContext context=this.getServletContext();
        TemplateEngine engine=(TemplateEngine)context.getAttribute("engine");
        WebContext webContext=new WebContext(req,resp,context);
        webContext.setVariable("images",images);
        engine.process("images",webContext,resp.getWriter());
    }

    private List<Image> loadImage() {
        List<Image> images=new ArrayList<>();
        ServletContext context=this.getServletContext();
        String path=context.getRealPath("/image");
        System.out.println(path);
        File imageRoot=new File(path);
        File[] files=imageRoot.listFiles();
        for (File f:files) {
            Image image=new Image();
            image.name=f.getName();
            image.url="image/"+f.getName();
            images.add(image);
        }
        return images;
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

    }
}
